package com.lechi.web.controller;

import com.lechi.web.framework.util.JwtTokenUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {


    @PostMapping("/token")
    public Map<String, Object> getDynamicToken(@RequestBody Map<String, String> request) {
        String staticToken = request.get("static_token");
        Map<String, Object> response = new HashMap<>();
        if (staticToken == null || !staticToken.startsWith("Bearer ") ||JwtTokenUtil.validateStaticToken(staticToken.substring(7)) == null) {
            response.put("message", "Invalid static token");
            response.put("status", 401);
        }
        String dynamicToken = JwtTokenUtil.getDynamicTokenByStaticToken(staticToken.substring(7));
        response.put("token", "Bearer " + dynamicToken);
        return response;
    }
}